--
-- Author: shunguo.chen
-- Date: 2017-11-29 10:07:32
--
-- MountStarView  坐骑星级界面

local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MountStarScene

local STAR_STR = L("lua_code_text_255")
local STAR_LV_STR = L("lua_code_text_213")
local STAR_ITEM_STR = L("notEnoughItemToRiseMountStar")

function ClassRef:init()
    display.setCascadeOpacityEnabled(self._root, true)
    self._ui.Button_cancel:addClickEventListener(function()
        self:closeView()
    end)
    self._ui.Button_up:addClickEventListener(function()
        if not self._lvOk then
            display.pushToast(STAR_LV_STR)
        elseif not self._itemOk then
            display.pushToast(STAR_ITEM_STR)
        else
            self:hide(function()
                executeFunc(self._onOperate, "starUp")
                executeFunc(self._onCloseView, self)
            end)
        end
    end)

    local con = self._ui["Panel/Panel_content"]

    -- attrs
    local dataCon = con:getChildByName("Panel_data")
    self._starText = dataCon:getChildByName("tlt_1")
    self._nextStarText = dataCon:getChildByName("tlt_2")
    self._attrText = dataCon:getChildByName("num_1")
    self._nextAttrText = dataCon:getChildByName("num_2")
    self._attrIcon = dataCon:getChildByName("icon_1")
    self._nextAttrIcon = dataCon:getChildByName("icon_2")

    -- items
    self._items = {0,0,0,0,0}
    for i=1,5 do
        local render = IconRender.create(con:findChild("Panel_item/item_"..i))
        render:setTipEnable(true)
        self._items[i] = render
    end

    self._lvText = con:findChild("Panel_gold/lv_num")

    -- guide
    self:checkGuide()
end

-- 检查是否触发了引导
function ClassRef:checkGuide()
    local params = {
        scene = Constant.FUNC_TYPE_MOUNT_STAR_UP,
        sceneSelf = self,
        getFunc = self.getGuideNodeFunc,
        nextFunc = self.checkGuide
    }
    EM:notify("checkGuide", params)
end

-- 获取引导node
function ClassRef:getGuideNodeFunc(id)
    if id == 156 then
        return self._ui.Button_up
    end
end

function ClassRef:getMount()
    return self._mount
end

function ClassRef:refresh(mount)
    self._mount = mount
    local star = mount:getStar()

    -- cur
    self._starText:setString(string.format(STAR_STR, star))
    local attrs = mount:getStarAttrs()
    for i,attr in ipairs(attrs) do
        local name, value, icon = GD:getPropShowData(attr[1], attr[2])
        self._attrText:setString(name .. " +" .. value)
        self._attrIcon:loadTexture(icon)
        break
    end

    -- next
    self._nextStarText:setString(string.format(STAR_STR, star+1))
    local attrs = GD:getMountStarAttrs(mount.starID + 1)
    for i,attr in ipairs(attrs) do
        local name, value, icon = GD:getPropShowData(attr[1], attr[2])
        self._nextAttrText:setString(name .. " +" .. value)
        self._nextAttrIcon:loadTexture(icon)
        break
    end

    self:refreshStarCollect()

    local needLv = mount:getStarUpNeedLv() or 0
    local playerLv = UD.player.level
    self._lvOk = playerLv >= needLv
    self._lvText:setString(needLv)
    self._lvText:setColor(self._lvOk and display.COLOR_GREEN or display.COLOR_RED)
end

function ClassRef:refreshStarCollect()
    self._itemOk = true
    local costItems = self._mount:getStarCostItems()
    for i,item in ipairs(self._items) do
        local data = costItems[i]
        item:setVisible(data ~= nil)
        if data then
            local needNum = data.amount or 1
            local hasNum = UD:getItemAmountByTid(data.tid)
            item:setData(data)
            item:setHasAmount(hasNum)
            self._itemOk = self._itemOk and (hasNum >= needNum)
        end
    end
end

return ClassRef